package com.sansec.demo.interfaces.facade;

import com.sansec.core.utils.IdUtils;
import com.sansec.core.utils.RocketMqUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * @author lichangwei
 */
@RestController
@RequestMapping(value = "rocketmq")
@Slf4j
public class RocketMqDemoController {

    @Autowired
    RocketMqUtils rocketMqUtils;

    @RequestMapping(value = "/send",method = RequestMethod.GET)
    public String send(){
        try{
            String msg= "this is a test msg "+ IdUtils.generator();
            Message message=new Message("test_topic","test_tag","test_key",msg.getBytes(StandardCharsets.UTF_8));
            log.info("rocket mq send msg:"+msg);
            rocketMqUtils.send(message);
        } catch (Exception e){
            log.error("很遗憾，发送失败了。",e);
            return "很遗憾，发送失败了。";
        }
        return "mq消息已发送";
    }
}
